/*
 * Copyright (C) 2019 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.xuexiang.templateproject.fragment.favorite;



import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.print.PrintAttributes;
import android.print.PrintDocumentAdapter;
import android.print.PrintJob;
import android.print.PrintManager;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.webkit.WebSettings;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.xuexiang.templateproject.R;
import com.xuexiang.templateproject.activity.SearchBLEActivity;
import com.xuexiang.templateproject.activity.SearchBTActivity;
import com.xuexiang.templateproject.adapter.FavoriteItemAdapter;
import com.xuexiang.templateproject.bean.QuestionBean;
import com.xuexiang.templateproject.core.BaseFragment;
import com.xuexiang.templateproject.databinding.FragmentFavoriteBinding;
import com.xuexiang.templateproject.databinding.FragmentFavoriteDetailBinding;
import com.xuexiang.templateproject.utils.DbUtils;
import com.xuexiang.templateproject.utils.PicUtils;
import com.xuexiang.xaop.annotation.SingleClick;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xui.widget.actionbar.TitleBar;
import com.xuexiang.xutil.app.ActivityUtils;
import com.xuexiang.xutil.common.logger.Logger;
import com.xuexiang.xutil.display.ImageUtils;

/**
 * @author xuexiang
 * @since 2019-10-16 10:25
 */
@Page(name = "收藏详细", extra = R.drawable.ic_expand_camera)
public class FavoriteDetailFragment extends BaseFragment<FragmentFavoriteDetailBinding> implements View.OnClickListener{

    @Override
    protected FragmentFavoriteDetailBinding viewBindingInflate(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, boolean attachToRoot) {
        return FragmentFavoriteDetailBinding.inflate(inflater, container, attachToRoot);
    }
//    @Override
//    protected TitleBar initTitle() {
//        return null;
//    }

    @Override
    protected void initViews() {
        Bundle arguments = getArguments();
        QuestionBean questionBean = DbUtils.getInstance().getQuestionById(arguments.getLong("id"));
        WebSettings webSettings = binding.webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setLoadsImagesAutomatically(true);
        webSettings.setDefaultTextEncodingName("utf-8");
        webSettings.setRenderPriority(WebSettings.RenderPriority.HIGH);
//        webSettings.setSupportMultipleWindows(true);
//        webSettings.setUseWideViewPort(true);
//        webSettings.setAppCacheMaxSize(5242880L);
//        webSettings.setAppCachePath(getActivity().getCacheDir().getAbsolutePath());
//        webSettings.setAllowFileAccess(false);
        webSettings.setTextSize(WebSettings.TextSize.NORMAL);
//        if (Build.VERSION.SDK_INT >= 16) {
//            webSettings.setAllowFileAccessFromFileURLs(false);
//            webSettings.setAllowUniversalAccessFromFileURLs(false);
//        }
        webSettings.setAppCacheEnabled(true);
//        webSettings.setSavePassword(false);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
//        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
//        webSettings.setDomStorageEnabled(true);
//        webSettings.setAllowContentAccess(false);
        this.binding.webView.setVerticalScrollbarOverlay(true);
//        this.binding.webView.setHorizontalScrollBarEnabled(true);
        binding.webView.setText(questionBean.getQuestionAndOption()+"\n"+questionBean.getAnswerAndAnalyze());
        binding.print.setOnClickListener(this);
    }


    @SingleClick
    @Override
    public void onClick(View view) {
        if(view.getId()==R.id.print){
           Logger.d("web "+ binding.webView.getText());
            String imgpath = getContext().getCacheDir()+"print_"+System.currentTimeMillis()+".png";
            ImageUtils.save(binding.webView,imgpath, Bitmap.CompressFormat.PNG);
            ActivityUtils.startActivity(SearchBTActivity.class,"imgPath",imgpath);

        }

    }
    private Bitmap createBitmapByView(View view) {
        //计算设备分辨率
        WindowManager manager = getActivity().getWindowManager();
        DisplayMetrics metrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(metrics);
        int width = metrics.widthPixels;
        int height = metrics.heightPixels;

        //测量使得view指定大小
        int measureWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
        int measureHeight = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.AT_MOST);

        view.measure(measureWidth, measureHeight);
        //调用layout方法布局后，可以得到view的尺寸
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

        final Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawColor(Color.WHITE);
        view.draw(canvas);
        return bitmap;
    }

}
